/**
 * Copyright 2007-2008 Vidar Svansson
 *
 * This file is part of JAdapter.
 *
 * JAdapter is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JAdapter is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with JAdapter.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.jadapter.registry;

import java.util.LinkedList;
import java.util.List;

import org.jadapter.AdapterFactory;
import org.jadapter.beans.UtilityBean;

/** A simple implementation of the ComponentRegistry interface. 
 * Nothing fancy here, simply a list of components that are iterated
 * on demand. Does not guarantee that the optimal component is returned. 
 */
public class SimpleComponentRegistry extends AbstractComponentRegistry {
    

    private List<AdapterFactory<Object,Object>> adapters;
    private List<Object> utilities;

  
	public int size() {
		return adapters.size() + utilities.size();
	}


	public SimpleComponentRegistry() {
        this(new LinkedList<AdapterFactory<Object,Object>>(), new LinkedList<Object>());
        
    }


    public SimpleComponentRegistry(List<AdapterFactory<Object, Object>> adapters,
            List<Object> utilities) {
        super();
        this.adapters = adapters;
        this.utilities = utilities;
    }


    /** Get an instance of a given type */
    @SuppressWarnings("unchecked") 
    public <T> T get(Class<T> type) {
        for(Object utility : utilities) {
            if(utility instanceof UtilityBean){
                UtilityBean util = (UtilityBean)utility;
                if(type.isAssignableFrom(util.provides())){
                    utility = util.getUtility();
                }
            }
            if(type.isInstance(utility))
                return (T)utility;
        }   
        return create(type);
    }
    
    
    /** Get an instance of a given type given some context type
     * 
     * @param <T> The return type
     * @param type The return type
     * @param context the type of the context
     * @return an instance of the required type or null
     */
    @SuppressWarnings("unchecked") 
    public  <T> T get(Class<T> type, Class<?> context){
        for(Object utility : utilities) {
            if(utility instanceof UtilityBean){
                UtilityBean util = (UtilityBean)utility;
                if(type.isAssignableFrom(util.provides()) 
                        && util.context() != null 
                        && context.isAssignableFrom(util.context()))
                    return (T)util.getUtility();
            }
        }   
        return null;        
    }     
       
    
    /** Create an instance of the give type for a context type */
    @SuppressWarnings("unchecked")
    public <T,C> T create(Class<T> type, Class<C> context) {
        for(AdapterFactory<?,?> adapter : adapters) {
            if(provides(adapter, type, context)){
                return (T)adapter.adapt(null);
            }
        }
        return null;
    }
       
    
    @SuppressWarnings("unchecked")
    public  <T> T adapt(Class<T> type, Object context, String name) {
        if(adapters == null)
            return self(type, context);
        for(AdapterFactory<Object,Object> adapter : adapters) {
            if(provides(adapter, type, context, name)){
                return (T)adapter.adapt(context);
            }
        }
        return self(type, context);
    }
    
    
    
    @SuppressWarnings("unchecked")
    public void register(AdapterFactory<?,?> adapter) {
        adapters.add((AdapterFactory)adapter);
    }     

    /** Register an object to serve as a singleton utility */
    public void register(Object utility){
        if(utility instanceof AdapterFactory)
            register((AdapterFactory<?,?>)utility);
        else utilities.add(utility);
    }

    /** Register an object to serve as a singleton utility for a specific interface
     * TODO 
     * @param type the type to provide
     * @param utility the instance singleton
     */
    public void register(Class<?> type, Object utility) {
        utilities.add(utility);
    }

  
    
}
